#include "misc.h"

void testExFlash()
{
	uint16_t flash_id = SPI_Flash_ReadID();
	char flashmsg[100];
	sprintf(flashmsg,"Flash ID is : %08x",flash_id);
	Debug(flashmsg);
	uint16_t flash_status = SPI_Flash_ReadSR();
	char flashmsg2[100];
	sprintf(flashmsg2,"Flash Status is : %02x",flash_status);
	Debug(flashmsg2);
}
void writeExFlash()
{
	//SPI_Flash_Erase_Chip();
	uint8_t testdata[16] = {0x01,0x02,0x33,0x23,0x18,0x25,0x02,0x33,0x3a,0x20,0x24,0x21,0x22,0x91,0x21	};
	SPI_Flash_Write(0x0 ,16 , testdata);

	uint8_t revData[20];
	memset(revData,0,20);
	SPI_Flash_Read( 0x0 ,16,revData);

	char debugmsg[100];
	memset(debugmsg,0,100);
	char datas[80];
	memset(datas,0,80);
	sprintf(debugmsg,"ExFlash read data:");
	char datax[10];
	for(int i = 0;i< 16;i++)
	{
		memset(datax,0,10);
		sprintf(datax,"%02xh,",revData[i]);
		sprintf(datas,"%s%s",datas,datax);
	}
	sprintf(debugmsg,"%s %s",debugmsg,datas);
	Debug(debugmsg);
}

FRESULT scan_files (char* path);

void writeTFCard(FATFS *fatfs,char *path)
{
		FIL file;
		FRESULT res;
		char tname[20];

		sprintf(tname,"%sdata.txt",path);
		//Debug(tname);
		res = f_open(&file, tname, FA_OPEN_ALWAYS|FA_READ|FA_WRITE );
		if(res!=FR_OK)
		{
			char msg[20];
			sprintf(msg,"err return is %d",res);
			
			Debug(msg);
			uartWrite("f_open() fail .. \r\n");
		}
		
		//else{
			//uartWrite("f_open() success .. \r\n");
	//	}
		res = f_lseek(&file,file.fsize);	

		uint8_t n = f_puts("hello world ..\r\n", &file) ;  //向文件末写入字符串
											
		if(n<1)  //判断写是否成功                        
		{                                                      
			uartWrite("f_puts() fail .. \r\n");                                             
		}
		f_close(&file);
		
#if F_READ

	while(1){									 //使用f_read读文件
		res = f_read(&file, buffer, 1, &br);     //一次读一个字节知道读完全部文件信息

		if (res == FR_OK )
		{
			printf("%s",buffer);
		}else{
		    printf("\r\n f_read() fail .. \r\n");	
		}

		if(f_eof(&file)) {break;}
	}

#endif
	
	
#if F_UNLINK

	res = f_unlink("test.jpg");	   //前提SD下存在一个test.jpg

	if(res!=FR_OK)
	{
		printf("\r\n f_unlink() fail .. \r\n");
	}else{
		printf("\r\n f_unlink() success .. \r\n");
	}

#endif

#if SCAN_FILES

	printf("\r\n the directory files : \r\n");
	scan_files("/");	      //扫描根目录

#endif
}

FRESULT scan_files (
    char* path        /* Start node to be scanned (also used as work area) */
)
{
    FRESULT res;
    FILINFO fno;
    DIR dir;
    int i;
    char *fn;   /* This function is assuming non-Unicode cfg. */
#if _USE_LFN
    static char lfn[_MAX_LFN + 1];
    fno.lfname = lfn;
    fno.lfsize = sizeof lfn;
#endif

    res = f_opendir(&dir, path);                       /* Open the directory */
    if (res == FR_OK) {
        i = strlen(path);
        for (;;) {
            res = f_readdir(&dir, &fno);                   /* Read a directory item */
            if (res != FR_OK || fno.fname[0] == 0) break;  /* Break on error or end of dir */
            if (fno.fname[0] == '.') continue;             /* Ignore dot entry */
#if _USE_LFN
            fn = *fno.lfname ? fno.lfname : fno.fname;
#else
            fn = fno.fname;
#endif
            if (fno.fattrib & AM_DIR) {                    /* It is a directory */
                sprintf(&path[i], "/%s", fn);
                res = scan_files(path);
                if (res != FR_OK) break;
                path[i] = 0;
            } else {                                       /* It is a file. */
                printf("\r\n %s/%s \r\n", path, fn);
            }
        }
    }

    return res;
}

